﻿using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Text;

namespace SZSLSystem.Common
{
    public class SMSHTTP
    {
        /// <summary>
        /// 请求的公共类用来向服务器发送请求
        /// </summary>
        /// <param name="strSMSRequest">发送请求的字符串</param>
        /// <returns>返回的是请求的信息</returns>
        private static string SMSrequest(string strSMSRequest)
        {
            string url = strSMSRequest;
            string html = "";
            try
            {
                HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                req.ContentType = "multipart/form-data";
                req.Accept = "*/*";
                req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
                req.Timeout = 30000;//30秒连接不成功就中断 
                req.Method = "GET";
                HttpWebResponse response = req.GetResponse() as HttpWebResponse;
                using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    html = sr.ReadToEnd();
                }
            }
            catch (WebException err)
            {
                html = err.ToString();
            }
            return html;
        }

        /// <summary>
        /// 注册接口
        /// </summary>
        /// <param name="CorpID">账号</param>
        /// <param name="Pwd">密码</param>
        /// <param name="CorpName">企业名称</param>
        /// <param name="LinkMan">联系人</param>
        /// <param name="Tel">联系电话</param>
        /// <param name="Mobile">联系人手机</param>
        /// <param name="Email">邮件</param>
        /// <param name="Memo">其他备注</param>
        /// <returns>返回值（0 注册成功 0 账号已经注册 –2 其他错误 –3 账号密码不匹配）</returns>
        public static string Reg(string CorpID, string Pwd, string CorpName, string LinkMan,
            string Tel, string Mobile, string Email, string Memo)
        {
            string Url = "http://inolink.com/WS/Reg.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd
                + "&CorpName=" + CorpName + "&LinkMan=" + LinkMan + "&Tel=" + Tel + "&Mobile=" + Mobile + "&Email=" + Email + "&Memo=" + Memo + "";
            return SMSrequest(Url);
        }

        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="CorpID">帐号</param>
        /// <param name="Pwd">密码</param>
        /// <param name="NewPwd">新密码</param>
        /// <returns>返回值（0 密码修改成功 –1 账号未注册 –2 其他错误 –3 密码错误 ）</returns>
        public static string UpPwd(string CorpID, string Pwd, string NewPwd)
        {
            string Url = "http://inolink.com/WS/UpdPwd.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + "&NewPwd=" + NewPwd + "";
            return SMSrequest(Url);
        }

        /// <summary>
        /// 修改注册信息
        /// </summary>
        /// <param name="CorpID">账号</param>
        /// <param name="Pwd">密码</param>
        /// <param name="CorpName">企业名称</param>
        /// <param name="LinkMan">联系人</param>
        /// <param name="Tel">联系电话</param>
        /// <param name="Mobile">联系人手机</param>
        /// <param name="Email">邮件</param>
        /// <param name="Memo">其他备注</param>
        /// <returns>返回值（0 修改成功 –1 账号未注册 –2 其他错误 –3 密码错误 ）</returns>
        public static string UpdReg(string CorpID, string Pwd, string CorpName, string LinkMan, string Tel, string Mobile, string Email, string Memo)
        {
            string Url = "http://inolink.com/WS/UpdReg.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + "&CorpName=" + CorpName
                + "&LinkMan=" + LinkMan + "&Tel=" + Tel + "&Mobile=" + Mobile + "&Email=" + Email + "&Memo=" + Memo + "";
            return SMSrequest(Url);
        }

        /// <summary>
        /// 查询余额
        /// </summary>
        /// <param name="CorpID">账号</param>
        /// <param name="Pwd">密码</param>
        /// <returns>返回值（0 剩余条数 -1 账号未注册 -2 其他错误 -3 密码错误 ）</returns>
        public static string SelSum(string CorpID, string Pwd)
        {
            string Url = "http://inolink.com/WS/SelSum.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + "";
            return SMSrequest(Url);
        }

        /// <summary>
        ///  发送短信
        /// </summary>
        /// <param name="CorpID">账号</param>
        /// <param name="Pwd">密码</param>
        /// <param name="Mobile">发送手机号码</param>
        /// <param name="Content">发送内容</param>
        /// <param name="Cell">子号(可为空)</param>
        /// <param name="SendTime">定时发送时间(为空表示及时短信),固定14位长度字符串，比如：20060912152435代表2006年9月12日15时24分35秒，为空表示立即发送</param>
        /// <returns>返回值（0 发送成功 -1 账号未注册 -2 其他错误 -3 密码错误 -4 手机号格式不对 -5 余额不足 -6 定时发送时间不是有效的时间格式 -7 禁止10小时以内向同一手机号发送相同短信 ）</returns>
        public static string Send(string CorpID, string Pwd, string Mobile, string Content, string Cell, string SendTime)
        {
            string Url = "http://inolink.com/WS/Send.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + "&Mobile=" + Mobile + "&Content=" + Content
                + "&Cell=" + Cell + "&SendTime=" + SendTime + " ";
            return SMSrequest(Url);
        }

        /// <summary>
        ///  发送短信
        /// </summary>
        /// <param name="CorpID">账号</param>
        /// <param name="Pwd">密码</param>
        /// <param name="Mobile">发送手机号码(多个号码以逗号分隔,最多支持600个号码)</param>
        /// <param name="Content">发送内容</param>
        /// <param name="Cell">子号(可为空)</param>
        /// <param name="SendTime">定时发送时间(为空表示及时短信),固定14位长度字符串，比如：20060912152435代表2006年9月12日15时24分35秒，为空表示立即发送</param>
        /// <returns>返回值（0 发送成功进入审核阶段 1 直接发送成功 -1 帐号尚未注册 -2 其他错误 -3 帐号或则密码错误 -4 一次提交信息不能超过600个手机号码 -5 企业号帐户余额不足，请先充值再提交短信息 -6 定时发送时间不是有效的时间格式 -8 发送内容需在3到250个字之间 -9 发送号码为空 ）</returns>
        public static string BatchSend(string CorpID, string Pwd, string Mobile, string Content, string Cell, string SendTime)
        {
            string Url = "http://inolink.com/WS/Send.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + "&Mobile=" + Mobile + "&Content=" + Content
                + "&Cell=" + Cell + "&SendTime=" + SendTime + " ";
            return SMSrequest(Url);
        }

        /// <summary>
        /// 接收短信
        /// </summary>
        /// <param name="CorpID">帐号</param>
        /// <param name="Pwd">密码</param>
        /// <returns>返回值（字符串 接受短信的内容 –1 账号未注册 –2 其他错误 –3 密码错误 ）</returns>
        public static string Get(string CorpID, string Pwd)
        {
            string Url = "http://inolink.com/WS/Get.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + " ";
            return SMSrequest(Url);
        }

        /// <summary>
        /// 帐号充值
        /// </summary>
        /// <param name="CorpID">帐号</param>
        /// <param name="Pwd">密码</param>
        /// <param name="CardNo">充值卡卡号</param>
        /// <param name="CardPwd">充值卡密码</param>
        /// <returns>返回值（0 充值成功 -1 账号未注册 -2 其他错误 -3 密码错误 -7 充值失败 ）</returns>
        public static string ChargeUp(string CorpID, string Pwd, string CardNo, string CardPwd)
        {
            string Url = "http://inolink.com/WS/ChargeUp.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + "&CardNo=" + CardNo 
                + "&CardPwd=" + CardPwd + " ";
            return SMSrequest(Url);
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="CorpID">帐号</param>
        /// <param name="Pwd">密码</param>
        /// <returns>返回值（0 注销成功 -1 账号未注册 -2 其他错误 -3 密码错误 ）</returns>
        public static string UnReg(string CorpID, string Pwd)
        {
            string Url = "http://inolink.com/WS/UnReg.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + " ";
            return SMSrequest(Url);
        }

        /// <summary>
        /// 获取错误报告
        /// </summary>
        /// <param name="CorpID">帐号</param>
        /// <param name="Pwd">密码</param>
        /// <param name="Mobilescell">扩展码</param>
        /// <returns>返回值（错误信息文本 失败 '||'+号码 +'#'+标题 +'#'+时间 +'#'+定时发送时间 +'#'+扩展码  成功 ）</returns>
        public static string GetReportFailMMS(string CorpID,string Pwd,string Mobilescell)
        {
            string Url = " http://inolink.com/WS/GetReportFailMMS.aspx?CorpID=" + CorpID + "&Pwd=" + Pwd + "&Cell= "+Mobilescell+"";
            return SMSrequest(Url);
        }
    }
}
